package com.ruix.app.mapper;

import com.ruix.app.model.Classes;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by hadexs on 2017/7/2.
 */

public class OrganizationListMapper implements ApiMapper<ArrayList<Classes>>{

    @Override
    public ArrayList<Classes> transform(JSONObject response) {
        ArrayList list = new ArrayList();
        if (response != null) {
            JSONObject data = response.optJSONObject("data");
            if (data != null) {
                JSONArray inarray = data.optJSONArray("in_list");//加入的圈子
                if (inarray != null && inarray.length() > 0) {
                    parseInClasses(list, inarray);
                }
//                JSONArray josNarray = data.optJSONArray("out_list");//未加入的圈子
//                if (josNarray != null && josNarray.length() > 0) {
//                    parseInClasses(list, josNarray);
//                }

            }
        }

        return list;
    }

    private void parseInClasses(ArrayList list ,JSONArray josNarray){
        for (int i = 0; i < josNarray.length();i++){
            JSONObject object =   josNarray.optJSONObject(i);
            if (object != null){
                Classes classes = new Classes();
                classes.setID(object.optString("id"));
                classes.setName(object.optString("name"));
                classes.setIcon(object.optString("icon"));
                classes.setType(object.optString("type"));
                classes.setLastPost(object.optString("last_post"));
                long lastUpdateTime = object.optLong("last_update",0)*1000;
                classes.setLastUpdate(lastUpdateTime);
                classes.setStatus(object.optString("status"));
                classes.setHaseNewMessage(object.optInt("new") == 1);
                list.add(classes);
            }
        }
    }




}
